import java.util.Random;
import java.util.Scanner;

public class TestDemo {
    //1.编写程序数一下 1到 100 的所有整数中出现多少个数字9
    public static void main1(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 10 == 9 || i / 10 == 9){
                sum++;
                if (i % 10 == 9 && i / 10 == 9){
                    sum++;
                }
            }
        }
        System.out.println(sum);
    }

    public static void main1_1(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 10 == 9){
                sum++;
            }
            if (i / 10 == 9){
                sum++;
            }
        }
        System.out.println(sum);
    }

    //2.输出 1000 - 2000 之间所有的闰年
    public static void main2(String[] args) {
        for (int i = 1000; i < 2000; i++) {
            if((i % 100 != 0 && i % 4 == 0) || i % 400 == 0){
                System.out.println(i);
            }
        }
    }

    //3.打印 1 - 100 之间所有的素数
    public static void main3(String[] args) {
        for (int i = 2; i < 100; i++) {
            int flag = 1;
            for (int j = 2; j <= (int)Math.sqrt(i); j++) {
                if (i % j == 0){
                    flag = 0;
                    break;
                }
            }
            if (flag == 1) {
                System.out.println(i);
            }
        }
    }

    //4.给定一个数字，判定一个数字是否是素数
    public static void main4(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int flag = 1;
        for (int i = 2; i <= (int)Math.sqrt(n); i++) {
            if (n % i == 0) {
                flag = 0;
                break;
            }
        }
        if (flag == 1) {
            System.out.println(n+"是素数");
        }
        else{
            System.out.println(n+"不是素数");
        }
    }
    public static void main4_1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int i = 2;
        for (; i <= Math.sqrt(n); i++) {
            if (n % i == 0){
                System.out.println(n + "不是素数");
                break;
            }
        }
        if (i > Math.sqrt(n)){
            System.out.println(n + "是素数");
        }
    }

    //5.根据输入的年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)
    public static void main5(String[] args) {
        Scanner input = new Scanner(System.in);
        int age = input.nextInt();
        if (age >= 0 && age <= 18){
            System.out.println("少年");
        }else if(age >= 19 && age <= 28){
            System.out.println("青年");
        }else if(age >= 29 && age <= 55) {
            System.out.println("中年");
        }else if(age >= 56 && age <= 200){
            System.out.println("老年");
        }else{
            System.out.println("请重新输入");
        }
    }

    //6.完成猜数字游戏 ，用户输入数字，判断该数字是大于，小于，还是等于随机生成的数字，等于的时候退出程序。
    public static void main6(String[] args) {
        Random random = new Random();
        Scanner input = new Scanner(System.in);
        //int n = (int)(Math.random() * 1000) % 100 + 1;//用户猜的
        int n = random.nextInt(100);//[0,100)
        while (true){
            System.out.println("请输入你要猜的数字：");
            int a = input.nextInt();//每一次都输入一次
            if (a > n){
                System.out.println("大了!");
            }else if (a < n){
                System.out.println("小了!");
            }else{
                System.out.println("Bingo!");
                break;
            }
        }
    }

    //7.求出0～999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数，其各位数字的立方和正好等于该数本 身，如；153＝1＋5＋3，则153是一个“水仙花数“。）
    public static void main7(String[] args) {
        for (int i = 100; i < 1000; i++) {
            int a = i % 10;
            int b = i / 10 % 10;
            int c = i / 100 % 10;
            if (i == a * a * a + b * b * b + c * c * c){
                System.out.println(i);
            }
        }
        findNum(99999);
    }
    //这里复杂亿点点, 算6位
    public static void findNum(int n){
        for (int i = 0; i <= n; i++) {
            int count = 0;
            int tmp = i;
            //先得到有多少位
            while(tmp != 0){
                count++;
                tmp /= 10;
            }
            //接着再换
            tmp = i;
            int sum = 0;
            while (tmp != 0){
                sum += Math.pow(tmp % 10, count);
                tmp /= 10;
            }
            if (sum == i){//结果相等，返回
                System.out.println(i);
            }
        }
    }

    //8.计算1/1 - 1/2 + 1/3 - 1/4 + 1/5 …… + 1/99 - 1/100 的值 。
    public static void main8(String[] args) {
        double sum = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 2 != 0){
                sum += 1.0 / i;//1.0不能忘
            }else{
                sum -= 1.0 / i;
            }
        }
        System.out.println(sum);
    }
    public static void main8_1(String[] args) {
        double sum = 0;
        int flg = 1;
        for (int i = 1; i <= 100; i++) {
            sum += 1.0 / i * flg;
            flg *= -1;
        }
        System.out.println(sum);
    }

    //9.求两个正整数的最大公约数
    public static void main9(String[] args) {
        int a = 16;
        int b = 24;
        int c = a < b ? a : b;
        int ret = 0;
        for (int i = c; i > 1; i--) {
            if (a % i == 0 && b % i == 0){//a % i不是 a % c
                ret = i;
                break;
            }
        }
        System.out.println("最大公约数为：" + ret);
    }
    //辗转相除法
    public static void main9_1(String[] args) {
        int a = 24;
        int b = 18;
        int c = a % b;
        while (c != 0){
            a = b;
            b = c;
            c = a % b;
        }
        System.out.println("最大公约数为" + b);
    }

    //10.求一个整数，在内存当中存储时，二进制1的个数。
    public static void main10(String[] args) {
        int n = 15;
        int num = 0;
        for (int i = 0; i < 32; i++) {
            if ((1 & (n >> i)) == 1){//不要和0比
                num++;
            }
        }
        System.out.println(num);
    }
    public static void main10_1(String[] args) {
        int n = 15;
        int num = 0;
        while(n != 0){
            if ((n & 1) == 1){
                num++;
            }
            n = n >> 1;
        }
        System.out.println(num);
    }
    public static void main10_2(String[] args) {
        int n = 15;
        int num = 0;
        while(n != 0){
            num++;
            n = n & (n - 1);
        }
        System.out.println(num);
    }

    //11.打印x型
    public static void main11(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j || i + j == n - 1) {
                    System.out.print("*");//注意不要换行
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    //多组输入
    public static void main11_1(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){//多组输入
            int n = sc.nextInt();
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (i == j || i + j == n - 1) {
                        System.out.print("*");//注意不要换行
                    } else {
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
        }
    }

    //12. 输出乘法口诀表
    public static void main12(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.printf("%d * %d = %d ", i, j, i * j);
            }
            System.out.println();
        }
    }

    //13.编写代码模拟三次密码输入的场景。 最多能输入三次密码，密码正确，提示“登录成功”,密码错误，
    // 可以重新输入，最多输入三次。三次均错，则提示退出程序
    public static void main13(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String passwd = "aa123456";
        for (int i = 3; i > 0 ; i--) {
            System.out.println("请输入密码：");
            String user = scanner.nextLine();
            if (passwd.equals(user)){
                System.out.println("登录成功");
                break;
            }else{
                System.out.println("错误,还有" + (i - 1) + "次机会");
            }
            if(i == 1)
                System.out.println("程序退出");
        }
    }

    //14.获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列
    //例如1011 偶数位01 奇数位11
    public static void main14(String[] args) {
        int a = 11;
        for (int i = 31; i >= 0 ; i--) {
            if (i % 2 == 0) {
                System.out.print((a >> i) & 1);
            }
        }
        System.out.println();
        for (int i = 31; i >= 0 ; i--) {
            if (i % 2 != 0) {
                System.out.print((a >> i) & 1);
            }
        }
    }
    public static void main14_1(String[] args) {
        int a = 11;
        for (int i = 30; i >= 0 ; i-=2) {
            System.out.print((a >> i) & 1);
        }
        System.out.println();
        for (int i = 31; i >= 1 ; i-=2) {
            System.out.print((a >> i) & 1);
        }
    }

    //15.输出一个整数的每一位
    public static void main15(String[] args) {
        int a = 6789;
        int[] b = new int[4];
        int i = 0;
        while(a != 0){
            int n = a % 10;
            a /= 10;
            b[i] = n;
            i++;
        }
        for (int j = 3; j >= 0 ; j--) {
            System.out.print(b[j] + " ");
        }
    }
    public static void main15_1(String[] args) {
        ////////////////////////////////////////////////
    }

    //16.输出一个整数的二进制每一位
    public static void main16(String[] args) {
        int a = 11;
        for (int i = 31; i >= 0; i--) {
            System.out.print((a >> i) & 1);
        }
    }

    //17.循环读取
    public static void main17(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextInt()){
            int n = scanner.nextInt();
            System.out.println(n);
        }
    }
    
}